package com.java.imauoj.Tools;

import com.java.imauoj.moudel.FanHuiZhi;
import com.java.imauoj.moudel.Field;
import com.java.imauoj.moudel.Filter;
import com.java.imauoj.moudel.Order;

import java.util.ArrayList;
import java.util.List;

public class Function {

    public FanHuiZhi qinjie(Filter filter){
        List<Field> filterAnd = filter.getFilterAnd();
        List<Field> filterOr = filter.getFilterOr();
        Order orderBy = filter.getOrderBy();
        String sql=" where ";
//        List list = new ArrayList();
        String[] list = new String[100];
        int i =0;
        for(Field a:filterAnd){
            if(a.getIs_like()==0){
                sql+=" problem." + a.getField() + "=#{list[" + i +"]} ";
//                list.add(a.getText());
                list[i] = "%"+a.getText()+"%";

            }else{
                sql+=" problem." + a.getField() + " like #{list[" + i +"]} ";
//                list.add("%"+a.getText()+"%");
                list[i] = "%"+a.getText()+"%";
            }
            sql+=" and ";
            i++;
        }

        for (Field a:filterOr){

            if(a.getIs_like()==0){
                sql+=" problem." + a.getField() + "=#{list[" + i +"]} ";
                list[i] = "%"+a.getText()+"%";

//                list.add(a.getText());
            }else{
                sql+=" problem." + a.getField() + " like #{list[" + i +"]} ";
//                list.add("%"+a.getText()+"%");
                list[i] = "%"+a.getText()+"%";
            }
            if(i==filterOr.size()-1) sql+=" and ";
            else sql+=" or ";
            i++;
        }
        sql+=" 1=1 order by problem."+ orderBy.getField()+" "+ orderBy.getMode();
        FanHuiZhi fanHuiZhi = new FanHuiZhi();
        fanHuiZhi.setFhz(list);
        fanHuiZhi.setSql(sql);
        return fanHuiZhi;
    }
}
